<?
/**
 * 쿠폰 (Coupon for Gnuboard4)
 *
 * Copyright (c) 2012 Choi Jae-Young <www.miwit.com>
 *
 * 저작권 안내
 * - 저작권자는 이 프로그램을 사용하므로서 발생하는 모든 문제에 대하여 책임을 지지 않습니다. 
 * - 이 프로그램을 어떠한 형태로든 재배포 및 공개하는 것을 허락하지 않습니다.
 * - 이 저작권 표시사항을 저작권자를 제외한 그 누구도 수정할 수 없습니다.
 */

include_once("_common.php");
include_once("../_config.php");
include_once("../_lib.php");

if ($is_admin != "super")
    alert_close("최고관리자만 접근할 수 있습니다.");

$coupon_info = sql_fetch("select * from $mw_coupon[info_table] where cp_id = '$cp_id'");
if (!$coupon_info)
    alert_close("데이터가 없습니다.");

$sql_common = " from $mw_coupon[list_table] ";
$sql_search = " where cp_id = '$cp_id' ";
$sql_order = " order by cp_use, cp_coupon ";

if ($stx)
    $sql_search .= " and cp_coupon like '%{$stx}%' ";

if ($use)
    $sql_search .= " and cp_use = '' ";

$sql = "select *
        $sql_common
        $sql_search
        $sql_order";
$qry = sql_query($sql);

$xls = "<table border=1>\n";
$xls .= "<tr>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 시작일 </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 종료일 </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 쿠폰번호 </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 사용여부 </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 사용일시 </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 사용IP </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 사용회원ID </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 사용회원별명 </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 지정회원ID </td>";
$xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> 지정회원별명 </td>";
$xls .= "</tr>\n";

$list = array();
while ($row = sql_fetch_array($qry)) {
    if ($row[cp_use_datetime] == '0000-00-00 00:00:00') $row[cp_use_datetime] = '';
    if ($row[cp_ip] == '') $row[cp_ip] = '';
    if ($row[mb_id] == '') $row[mb_id] = '';
    if ($row[mb_id_only] == '') $row[mb_id_only] = '';

    $row[name] = '';
    $row[name_only] = '';

    if ($row[mb_id]) {
        $tmp = get_member($row[mb_id], "mb_id, mb_nick, mb_email, mb_homepage");
        $row[name] = $tmp[mb_nick]; //get_sideview($tmp[mb_id], $tmp[mb_nick], $tmp[mb_email], $tmp[mb_homepage]);
    }

    if ($row[mb_id] != $row[mb_id_only]) {
        $tmp = get_member($row[mb_id_only], "mb_id, mb_nick, mb_email, mb_homepage");
        $row[name_only] = $tmp[mb_nick]; //get_sideview($tmp[mb_id], $tmp[mb_nick], $tmp[mb_email], $tmp[mb_homepage]);
    } else {
        $row[name_only] = $row[name];
    }
 
    $xls .= "<tr>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $coupon_info[cp_sdate] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $coupon_info[cp_edate] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $row[cp_coupon] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $row[cp_use] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $row[cp_use_datetime] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $row[cp_ip] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $row[mb_id] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $row[name] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $row[mb_id_only] </td>";
    $xls .= "<td style=\"mso-number-format:'\\@';mso-style-parent:style0;\"> $row[name_only] </td>";
    $xls .= "</tr>\n";
}

$xls .= "</table>\n";

$filename = "쿠폰-$coupon_info[cp_subject]-".date("ymd", $g4[server_time]).".xls";
if (preg_match("/^utf/i", $g4[charset])) {
    $filename = urlencode($filename);
}

header( "Content-type: application/vnd.ms-excel" ); 
header( "Content-Disposition: attachment; filename=$filename" ); 

echo ("<meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel; charset={$g4[charset]}\">"); 
echo $xls;
exit;
